<?php
/**
 * 图片库
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

class Imgallery extends Field
{
    public static $html = <<<EOF
    <div class="form-group">
        <label for="[FIELD_NAME]" class="col-sm-2 control-label">[FORM_NAME]</label>
        <input type="hidden" id ="[FIELD_NAME]" value="{\$data.[FIELD_NAME]|default=''}" name ="[FIELD_NAME]">
        <div class="col-sm-10 col-md-4" onClick="GetUploadify(1,'','user','img_call_back')">
            <img id="imgs" src="{\$data.[FIELD_NAME]|default='/static/admin/css/images/image.png}" style="width: 150px;">
        </div>
    </div>
    <script>
        function GetUploadify(){
           //iframe层
           parent.layer.open({
                type: 2,
                title: '上传图片',
                shadeClose: true,
                shade: 0.8,
                area: ['60%', '90%'],
                content: '/admin/img/index' //iframe的url
           });
        }
        //上传图片回调事件
        function img_call_back(fileurl_tmp) {
            $("#img").val(fileurl_tmp);
            $("#imgs").attr("src",fileurl_tmp);
            $("#cover_img").attr("value",fileurl_tmp);
        }
    </script>\n
EOF;
    public static $rules = [
        'required'   => '非空',
        'file_size'  => '文件大小限制',
        'file_image' => '图片类型',
        'regular'    => '自定义正则'
    ];
    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace(array('[FORM_NAME]', '[FIELD_NAME]'), array($data['form_name'], $data['field_name']), $html);
        return $html;
    }
}